package dao;

import java.io.Serializable;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


public class DaoImpl implements Dao {  

    @Autowired  
    SessionFactory sessionFactory;
    
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    
    public void setSessionFactory(SessionFactory sessionFactory) {
         this.sessionFactory = sessionFactory;
    }
    
    @Transactional  
    public void persist(Object entity) {  
    	getSessionFactory().getCurrentSession().saveOrUpdate(entity);  
    }  

    @Transactional  
    public void persist(Object[] entities) {  
        for (Object object : entities) {  
            persist(object);  
        }  
    }  

    @Transactional(readOnly = true)  
    public <T> List<T> find(Class<T> entityClass) {  
        final List<T> entities = getSessionFactory().getCurrentSession().createCriteria(entityClass).list();
        return entities;  
    }  

    @Transactional(readOnly = true)  
    public <T> T load(Class<T> entityClass, Serializable id) {  
        final T entity = (T)getSessionFactory().getCurrentSession().load(entityClass, id);  
        return entity;  
    }  

    @Transactional(readOnly = true)  
    public <T> List<T> find(String hql) {  
        final List<T> entities = getSessionFactory().getCurrentSession().createQuery(hql).list();  
        return entities;  
    }
    
    @Transactional(readOnly = false)
    public void delete(Object entity){
    	getSessionFactory().getCurrentSession().delete(entity);
    }


}  